home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / navigator / linkToolbarOverlay.js < prev    next >
Encoding:
JavaScript  |  2002-04-10  |  7.9 KB  |  243 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Eric Hodel's <drbrain@segment7.net> code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Eric Hodel.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *      Christopher Hoess <choess@force.stwing.upenn.edu>
  23.  *      Tim Taylor <tim@tool-man.org>
  24.  *      Henri Sivonen <henris@clinet.fi>
  25.  *      Stuart Ballard <sballard@netreach.net>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the MPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the MPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. var LinkToolbarUI = function()
  42. {
  43. }
  44.  
  45. LinkToolbarUI.prototype.linkAdded =
  46. function(event)
  47. {
  48.   var element = event.originalTarget;
  49.  
  50.   if (element.ownerDocument != getBrowser().contentDocument ||
  51.       !linkToolbarUI.isLinkToolbarEnabled() ||
  52.       !element instanceof Components.interfaces.nsIDOMHTMLLinkElement ||
  53.       !element.href || (!element.rel && !element.rev))
  54.     return;
  55.  
  56.   linkToolbarHandler.handle(element);
  57. }
  58.  
  59. LinkToolbarUI.prototype.isLinkToolbarEnabled =
  60. function()
  61. {
  62.   if (document.getElementById("linktoolbar").getAttribute("hidden") == "true")
  63.     return false;
  64.   else
  65.     return true;
  66. }
  67.  
  68. LinkToolbarUI.prototype.clear =
  69. function(event)
  70. {
  71.   if (event.originalTarget != getBrowser().contentDocument ||
  72.       !linkToolbarUI.isLinkToolbarEnabled() ||
  73.       !linkToolbarHandler.hasItems)
  74.     return;
  75.  
  76.   linkToolbarHandler.clearAllItems();
  77. }
  78.  
  79. LinkToolbarUI.prototype.fullSlowRefresh =
  80. function()
  81. {
  82.   var currentNode = getBrowser().contentDocument.documentElement;
  83.   if (!(currentNode instanceof Components.interfaces.nsIDOMHTMLHtmlElement))
  84.     return;
  85.   currentNode = currentNode.firstChild;
  86.   
  87.   while(currentNode)
  88.   {
  89.     if (currentNode instanceof Components.interfaces.nsIDOMHTMLHeadElement) {
  90.       currentNode = currentNode.firstChild;
  91.       
  92.       while(currentNode)
  93.       {
  94.         if (currentNode instanceof Components.interfaces.nsIDOMHTMLLinkElement)
  95.           linkToolbarUI.linkAdded({originalTarget: currentNode});
  96.         currentNode = currentNode.nextSibling;
  97.       }
  98.     }
  99.     else if (currentNode instanceof Components.interfaces.nsIDOMElement)
  100.     {
  101.       // head is supposed to be the first element inside html.
  102.       // Got something else instead. returning
  103.        return;
  104.     }
  105.     else
  106.     {
  107.       // Got a comment node or something like that. Moving on.
  108.       currentNode = currentNode.nextSibling;
  109.     }
  110.   }  
  111. }
  112.  
  113. LinkToolbarUI.prototype.toolbarActive = false;
  114.  
  115. LinkToolbarUI.prototype.activate =
  116. function()
  117. {
  118.   if (!linkToolbarUI.toolbarActive) {
  119.     linkToolbarUI.toolbarActive = true;
  120.     document.getElementById("linktoolbar").setAttribute("hasitems", "true");
  121.     var contentArea = document.getElementById("appcontent");
  122.     contentArea.addEventListener("unload", linkToolbarUI.clear, true);
  123.     contentArea.addEventListener("load", linkToolbarUI.deactivate, true);
  124.     contentArea.addEventListener("DOMHeadLoaded", linkToolbarUI.deactivate,
  125.                                  true);
  126.   }
  127. }
  128.  
  129. LinkToolbarUI.prototype.deactivate =
  130. function()
  131. {
  132.   // This function can never be called unless the toolbar is active, because
  133.   // it's a handler that's only activated in that situation, so there's no need
  134.   // to check toolbarActive. On the other hand, by the time this method is
  135.   // called the toolbar might have been populated again already, in which case
  136.   // we don't want to deactivate.
  137.   if (!linkToolbarHandler.hasItems) {
  138.     linkToolbarUI.toolbarActive = false;
  139.     document.getElementById("linktoolbar").setAttribute("hasitems", "false");
  140.     var contentArea = document.getElementById("appcontent");
  141.     contentArea.removeEventListener("unload", linkToolbarUI.clear, true);
  142.     contentArea.removeEventListener("load", linkToolbarUI.deactivate, true);
  143.     contentArea.removeEventListener("DOMHeadLoaded", linkToolbarUI.deactivate,
  144.                                     true);
  145.   }
  146. }
  147.  
  148. /* called whenever something on the toolbar is clicked */
  149. LinkToolbarUI.prototype.clicked =
  150. function(event)
  151. {
  152.   // Only handle primary click.  Change this if we get a context menu
  153.   if (0 != event.button) return;  
  154.  
  155.   // Return if this is one of the menubuttons.
  156.   if (event.target.getAttribute("type") == "menu") return;
  157.   
  158.   if (!event.target.getAttribute("href")) return;
  159.  
  160.   var destURL = event.target.getAttribute("href");
  161.   
  162.   // We have to do a security check here, because we are loading URIs given
  163.   // to us by a web page from chrome, which is privileged.
  164.   try {
  165.     var ssm = Components.classes["@mozilla.org/scriptsecuritymanager;1"].getService().
  166.                     QueryInterface(Components.interfaces.nsIScriptSecurityManager);
  167.       ssm.checkLoadURIStr(window.content.location.href, destURL, 0);
  168.     var referrer =
  169.         Components.classes["@mozilla.org/network/standard-url;1"].
  170.           createInstance(Components.interfaces.nsIURI);
  171.     referrer.spec = window.content.location.href;
  172.     loadURI(destURL, referrer);
  173.   } catch (e) {
  174.     dump("Error: it is not permitted to load this URI from a <link> element: " + e);
  175.   }
  176. }
  177.  
  178. // functions for twiddling XUL elements in the toolbar
  179.  
  180. LinkToolbarUI.prototype.toggleLinkToolbar =
  181. function(checkedItem)
  182. {
  183.   this.goToggleTristateToolbar("linktoolbar", checkedItem);
  184.   this.initHandlers();
  185.   if (this.isLinkToolbarEnabled())
  186.     this.fullSlowRefresh();
  187.   else
  188.     linkToolbarHandler.clearAllItems();
  189. }
  190.  
  191. LinkToolbarUI.prototype.initLinkbarVisibilityMenu = 
  192. function()
  193. {
  194.   var state = document.getElementById("linktoolbar").getAttribute("hidden");
  195.   if (!state)
  196.     state = "maybe";
  197.   var checkedItem = document.getElementById("cmd_viewlinktoolbar_" + state);
  198.   checkedItem.setAttribute("checked", true);
  199.   checkedItem.checked = true;
  200. }
  201.  
  202. LinkToolbarUI.prototype.goToggleTristateToolbar =
  203. function(id, checkedItem)
  204. {
  205.   var toolbar = document.getElementById(id);
  206.   if (toolbar)
  207.   {
  208.     toolbar.setAttribute("hidden", checkedItem.value);
  209.     document.persist(id, "hidden");
  210.   }
  211. }
  212.  
  213. LinkToolbarUI.prototype.addHandlerActive = false;
  214.  
  215. LinkToolbarUI.prototype.initHandlers =
  216. function()
  217. {
  218.   var contentArea = document.getElementById("appcontent");
  219.   if (linkToolbarUI.isLinkToolbarEnabled())
  220.   {
  221.     if (!linkToolbarUI.addHandlerActive) {
  222.       contentArea.addEventListener("DOMLinkAdded", linkToolbarUI.linkAdded,
  223.                                    true);
  224.       linkToolbarUI.addHandlerActive = true;
  225.     }
  226.   } else
  227.   {
  228.     if (linkToolbarUI.addHandlerActive) {
  229.       contentArea.removeEventListener("DOMLinkAdded", linkToolbarUI.linkAdded,
  230.                                       true);
  231.       linkToolbarUI.addHandlerActive = false;
  232.     }
  233.   }
  234.   if (!linkToolbarUI.initialized)
  235.   {
  236.     linkToolbarUI.initialized = true;
  237.     document.removeEventListener("load", linkToolbarUI.initHandlers, true);
  238.   }
  239. }
  240.  
  241. const linkToolbarUI = new LinkToolbarUI;
  242.  
  243.